import { createApp } from 'vue'
import App from './App.vue'
import { store, key } from './store'
import router from './router'
import ElementPlus, { ElNotification } from 'element-plus'
import 'element-plus/dist/index.css'
import '@/assets/css/common.scss'
import * as ElIcons from '@element-plus/icons'
import { start, close } from '@/script/utils/nprogress'

// createApp(App).mount('#app')

const app = createApp(App)

Object.keys(ElIcons).forEach((key) => {
    app.component(key, ElIcons[key])
})

app.use(ElementPlus)

app.use(store, key)
app.use(router)
app.mount('#app')

// 设置路由导航
router.beforeEach((to, from, next) => {
    start()
    // console.log(to, from, next)
    document.title = 'WAVE BLOG'
    if (to.meta.title) {
        document.title = document.title + ' — ' + String(to.meta.title)
    }
    // 路由出错处理
    if (to.matched.length === 0 && !to.name) {
        ElNotification({
            title: '路由地址错误！',
            message: `【${to.fullPath}】 路径找不到对应页面，切换到首页！`,
            type: 'error',
            duration: 3000,
        })
        router.push('/')
    }

    next()
})

// 路由加载结束后运行
router.afterEach(() => {
    close()
})
